package com.cong.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author cong
 * <p>
 * created on 2021/3/31 上午10:59
 */
public class SynchronousQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"put aaa");
                blockingQueue.put("aaa");
                System.out.println(Thread.currentThread().getName()+"put bbb");
                blockingQueue.put("bbb");
                System.out.println(Thread.currentThread().getName()+"put ccc");
                blockingQueue.put("ccc");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"A").start();
        new Thread(()->{
            try {

                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+blockingQueue.take());
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+blockingQueue.take());
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+blockingQueue.take());
            }catch (Exception e){
                e.printStackTrace();
            }

        },"B").start();
    }
}
